package com.zlk.algorithm.dataStructure.linkList03;
/**
 * @program: algorithm
 * @ClassName Code04_RingArray
 * @description:用环形数组实现栈和队列
 * @author: slfang
 * @create: 2024-12-12 11:10
 * @Version 1.0
 **/
public class Code04_RingArray {


    class CircleQueue{
        public int beginIndex = 0;
        public int endIndex = 0;
        public int limit = 10;//限制数组的容量

        public int[] arr = new int[limit];

        public int size ;//目前已添加的长度

        void push(int val) {
            if(size==limit){
                System.out.println("数组已满");
            }
            size++;
            arr[endIndex] = val;
            endIndex = nextIndex(endIndex);

        }

        /**
         * 不能只是简单++ 因为是循环数组，如果栈填出的话，前方数据会留位置
         */

        private int nextIndex(int i) {
            return i < limit - 1 ? i + 1 : 0;
        }

        int poll(){
            if(size==0){
                return -1;
            }else{
                int i = arr[beginIndex];
                size--;
                beginIndex = nextIndex(beginIndex);
                return i;
            }
        }
    }



    class CircleStack {
        public int limit = 10;//限制数组的容量

        public int[] arr = new int[limit];

        public int size ;//目前已添加的长度

        void push(int val) {
            if(size==limit-1){
                System.out.println("数组已满");
            }
            arr[size++] = val;
        }

        int poll(){
            if(size==0){
                return -1;
            }else{
                return arr[--size];
            }
        }
    }
}
